<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Hierarchy
 *
 * @author Cata
 */
class Hierarchy {
    public $Id;
    public $IdHash;
    public $ParentId;
    public $CreateDate;
    public $LastUpdateDate;
    public $Ip;
    
    public static function findById($id){
        require_once 'script.php';
        $sql = "SELECT * FROM hierarchy WHERE Id = " . $id;
        $query= mysql_query($sql);
        $res = mysql_fetch_array($query);
        $file = new Hierarchy();
        $file->Id = $res['Id'];
        $file->IdHash = $res['IdHash'];
        $file->ParentId = $res['ParentId'];        
        $file->CreateDate = $res['CreateDate'];
        $file->LastUpdateDate = $res['LastUpdateDate'];
        $file->Ip = $res['Ip'];
        return $file;      
    }
    
    public function save(){
        require_once 'script.php';
        
        $sql = "INSERT INTO hierarchy SET
                    IdHash = '" . $this->IdHash . "',
                    CreateDate = '" . $this->CreateDate . "',
                    LastUpdateDate = '" . $this->CreateDate . "',
                    Ip = '" . $this->Ip . "',
                    ParentId = '" . $this->ParentId . "'";
        $query = mysql_query($sql);
        $this->Id = mysql_insert_id();
        $sql = "UPDATE hierarchy SET
                    IdHash = '" . $this->IdHash . "," . $this->Id . "'
                WHERE Id = " . $this->Id;
        $query = mysql_query($sql);
    }
    
    public static function getCurrentHierarchies($id){ // we get all hierarchies of one parent
        $sql = "SELECT * FROM hierarchy WHERE ParentId = " . $id;
        $query=  mysql_query($sql);
        $index = 0;
        while ($res = mysql_fetch_array($query)){
            $hierarchies[$index] = new Hierarchy();
            $hierarchies[$index]->Id = $res['Id'];
            $hierarchies[$index]->IdHash = $res['IdHash'];
            $hierarchies[$index]->ParentId = $res['ParentId'];        
            $hierarchies[$index]->CreateDate = $res['CreateDate'];
            $hierarchies[$index]->LastUpdateDate = $res['LastUpdateDate'];
            $hierarchies[$index]->Ip = $res['Ip'];
            $index++;
        }
        return $hierarchies;
    }
    
    public static function findAllChildren($id){ //find all file info that are children for the current file
        $sql = "SELECT * FROM hierarchy WHERE IdHash like '%" . $id . "%'";
        $result = mysql_query($sql);
        $index=0;
        while($res = mysql_fetch_array($result)){
            $files[$index] = File::findByHierarchy($res['Id']); 
            $index++;
        }
        return $files;
    }
    
    public function deleteAll(){
        if($this->hasChildren()){ // the hierarchy has children, we delete recursively them
            $children = $this->getChildren();
            foreach($children as $child){
                $hierarchy = Hierarchy::findById($child);
                $hierarchy->deleteAll();
            }
            $this->delete();
        } else {               // else we delete the file
            $this->delete();
        }
    }
    
    public function delete(){
        $sql = "DELETE FROM hierarchy WHERE Id = ". $this->Id;
        $query = mysql_query($sql);
    }
    
    public function hasChildren(){       // test if a hierarchy has children
        $sql = "SELECT * FROM hierarchy WHERE ParentId = " .  $this->Id;
        $result = mysql_query($sql);
        if(mysql_num_rows($result)){
            return true;
        } else {
            return false;
        }
    }
    
    public function getChildren(){  // return hierarchies that are children for the current one
        $sql = "SELECT * FROM hierarchy WHERE ParentId = " .  $this->Id;
        $result = mysql_query($sql);
        $index = 0;
        while($res = mysql_fetch_array($result)){
            $child[$index] = $res['Id'];
            $index++;
        }
        return $child;
    }
    public function copyAll($destinationHierarchy){
        $copyfile = File::findByHierarchy($this->Id);
        if ($this->hasChildren()){
            $newHierarchy = new Hierarchy();
            $newHierarchy->IdHash = $destinationHierarchy->IdHash;
            $newHierarchy->ParentId = $destinationHierarchy->Id;        
            $newHierarchy->CreateDate = date('y-m-d H:i:s');
            $newHierarchy->LastUpdateDate = date('y-m-d H:i:s');
            $newHierarchy->Ip = $_SERVER['REMOTE_ADDR'];
            $newHierarchy->save();
            
            $newHierarchySaved = Hierarchy::findById($newHierarchy->Id);
            $children = $this->getChildren();
            foreach($children as $child){
                $hierarchy = Hierarchy::findById($child);
                $hierarchy->copyAll($newHierarchySaved);
            }
            $newFile = new File ();
            $newFile->Name = $copyfile->Name;
            $newFile->HierarchyId = $newHierarchySaved->Id;
            $newFile->CreateDate = date('y-m-d H:i:s');
            $newFile->LastUpdateDate = date('y-m-d H:i:s');
            $newFile->Ip = $_SERVER['REMOTE_ADDR'];
            $newFile->Size = $copyfile->Size;
            $newFile->FileTypeId = $copyfile->FileTypeId;
            $newFile->save();
        } else {
            $newHierarchy = new Hierarchy();
            $newHierarchy->IdHash = $destinationHierarchy->IdHash;
            $newHierarchy->ParentId = $destinationHierarchy->Id;        
            $newHierarchy->CreateDate = date('y-m-d H:i:s');
            $newHierarchy->LastUpdateDate = date('y-m-d H:i:s');
            $newHierachy->Ip = $_SERVER['REMOTE_ADDR'];
            $newHierarchy->save();
            $newHierarchySaved = Hierarchy::findById($newHierarchy->Id);
            
            $newFile = new File ();
            $newFile->Name = $copyfile->Name;
            $newFile->HierarchyId = $newHierarchySaved->Id;
            $newFile->CreateDate = date('y-m-d H:i:s');
            $newFile->LastUpdateDate = date('y-m-d H:i:s');
            $newFile->Ip = $_SERVER['REMOTE_ADDR'];
            $newFile->Size = $copyfile->Size;
            $newFile->FileTypeId = $copyfile->FileTypeId;
            $newFile->save();
        }

    }
}

?>
